/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import GUI.Admin.AdminBookingPickerDialog;
import dao.DatChoDAO;
import java.awt.Color;
import javax.swing.JButton;
import main.MainFormApp;
import pojo.Chongoi;
import pojo.Datcho;
import pojo.Lichchieuphim;

/**
 *
 * @author Hnmtuan
 */
public class BookingButton extends JButton {
    // <editor-fold defaultstate="collapsed" desc="Define all variables using this application">

    Chongoi _ownedPlace;
    Lichchieuphim _ownedSchedule;
    AdminBookingPickerDialog _ownedPicker;
    Datcho _ownerBooking;
    // </editor-fold>
    //<editor-fold defaultstate="collapsed" desc="Define messages and error messages" >
    private final String MSG_DEFAULT = "Unknown";
    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Define Constructor" >

    public BookingButton(Chongoi paramPlace, Lichchieuphim paramSchedule) {
        _ownedPlace = paramPlace;
        _ownedSchedule = paramSchedule;
        setLabelButton();
        setOwnedColor();
        setEventForButton();
    }

    // </editor-fold>
    // <editor-fold defaultstate="collapsed" desc="Implement All Property or State">
    public Lichchieuphim getOwnedSchedule() {
        return _ownedSchedule;
    }

    public void setOwnedSchedule(Lichchieuphim _ownedSchedule) {
        this._ownedSchedule = _ownedSchedule;
    }

    public Chongoi getOwnedPlace() {
        return _ownedPlace;
    }

    public void setOwnedPlace(Chongoi _ownedPlace) {
        this._ownedPlace = _ownedPlace;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Implement Actions to GUI" >
    protected final void setLabelButton() {
        String label = (_ownedPlace != null) ? String.format("P%d", _ownedPlace.getViTriChoNgoi()) : MSG_DEFAULT;
        setText(label);
    }

    protected final void setOwnedColor() {
        if (_ownedPlace == null) {
            return;
        }
        try {
            if (_ownedPlace.getDatchos().size() < 1) {
                return;
            }
            Object cachedObject = ClassUtils.getFirstItemInSet(_ownedPlace.getDatchos());
            _ownerBooking = (cachedObject instanceof Datcho) ? (Datcho) cachedObject : null;
            setBackground(Color.RED);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected final void setEventForButton() {
        if (_ownedPlace == null) {
            return;
        }
        addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    if (_ownedSchedule == null || _ownedPlace == null) {
                        return;
                    }
                    _ownedPicker = new AdminBookingPickerDialog(null, true);
                    MainFormApp.getApplication().show(_ownedPicker);
                    Lichchieuphim localSchedule = _ownedSchedule;
                    Chongoi localPlace = _ownedPlace;
                    if (!_ownedPicker.isBooking()) {
                        Datcho cachedObject = _ownerBooking;
                        if (cachedObject != null) {
                            DatChoDAO.deleteDatCho(cachedObject);
                        }
                        return;
                    }
                    Datcho booking = new Datcho(localPlace, localSchedule, DatChoDAO.generateCodePlace(),
                            null, ClassUtils.StateFields.DEFAULT_STATUS);

                    DatChoDAO.insertDatCho(booking);
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    setVisible(false);
                }
            }
        });
    }
    // </editor-fold>
}
